package com.daxt.controller.warehouse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.warehouse.WarehouseShelfPostionFileMapper;
import com.daxt.model.service.archives.vo.ArchivesDoc;
import com.daxt.model.service.warehouse.result.FileCount;
import com.daxt.model.service.warehouse.vo.WarehouseShelfPostionFile;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "文件")
@RestController
@RequestMapping("/warehouse/file")
public class WarehouseShelfPostionFileController {
	
	@Autowired 
	private WarehouseShelfPostionFileMapper warehouseShelfPostionFileMapper;
	
	@ApiOperation(value = "文件列表", notes = "") 
	@GetMapping("/list/{id}") 
	public Result<List<ArchivesDoc>> list(@PathVariable("id")String id){
		Map<String,Object> param=new HashMap<String, Object>();
		param.put("pId", id);
		List<ArchivesDoc> docList = warehouseShelfPostionFileMapper.queryList(param);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, docList==null?new ArrayList<ArchivesDoc>():docList);
	}
	
	@ApiOperation(value = "文件总数", notes = "") 
	@GetMapping("/count/{id}") 
	public Result<FileCount> fileCount(@PathVariable("id")String id){
		Map<String,Object> param=new HashMap<String, Object>();
		param.put("pId", id);
		param.put("fileType", "dossier");
		
		Integer dossierCount = warehouseShelfPostionFileMapper.selectFileCount(param);
		Map<String,Object> param1=new HashMap<String, Object>();
		param1.put("pId", id);
		param1.put("fileType", "doc");
		Integer docCount = warehouseShelfPostionFileMapper.selectFileCount(param1);
		FileCount fileCount=new FileCount();
		fileCount.setDossierCount(dossierCount==null?0:dossierCount);
		fileCount.setDocCount(docCount==null?0:docCount);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, fileCount);
	}
	
	@ApiOperation(value = "文件添加", notes = "") 
	@PostMapping("") 
	public Result<String> save(@RequestBody List<WarehouseShelfPostionFile> entitys){
		for (WarehouseShelfPostionFile warehouseShelfPostionFile : entitys) {
			QueryWrapper<WarehouseShelfPostionFile> queryWrapper=new QueryWrapper<WarehouseShelfPostionFile>();
			queryWrapper.eq("pId", warehouseShelfPostionFile.getPId());
			queryWrapper.eq("fileId", warehouseShelfPostionFile.getFileId());
			List<WarehouseShelfPostionFile> list = warehouseShelfPostionFileMapper.selectList(queryWrapper);
			if(list!=null&&list.size()>0)
				continue;
			warehouseShelfPostionFileMapper.insert(warehouseShelfPostionFile);
		}
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "文件修改", notes = "") 
	@PutMapping("") 
	public Result<String> update(@RequestBody WarehouseShelfPostionFile entity){
		QueryWrapper<WarehouseShelfPostionFile> queryWrapper=new QueryWrapper<WarehouseShelfPostionFile>();
		queryWrapper.eq("pId", entity.getPId());
		queryWrapper.eq("fileId", entity.getFileId());
		List<WarehouseShelfPostionFile> list = warehouseShelfPostionFileMapper.selectList(queryWrapper);
		if(list!=null&&list.size()>0) {
			ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "文件以在该位置添加过！！！", null);
		}
		warehouseShelfPostionFileMapper.updateById(entity);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "文件删除", notes = "") 
	@DeleteMapping("") 
	public Result<String> del(@RequestBody List<String> ids){
		QueryWrapper<WarehouseShelfPostionFile> wrapper=new QueryWrapper<WarehouseShelfPostionFile>();
		wrapper.in("fileId", ids);
		warehouseShelfPostionFileMapper.delete(wrapper);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	
	
}
